package com.runchinaup.sifitbledome.ble;

import android.util.Log;

import com.runchinaup.sifitbledome.ble.conn.ConnResult;
import com.runchinaup.sifitbledome.domain.SendData;

/**
 * Created by liber on 2018/3/16.
 */

public class DataInterface implements BleHelper.BleListener {

    private BleHelper bleHelper = BleHelper.getBleHelper();

    private static DataInterface dataInterface;

    private DataInterface() {
    }

    public static DataInterface getDataInterface() {
        if (dataInterface == null) {
            dataInterface = new DataInterface();
        }
        return dataInterface;
    }

    //断开连接
    public void disconn() {
        bleHelper.disConn();
    }

    //发送数据
    public void sendToBle(SendData sendData) {
        bleHelper.sendData(sendData);
    }

    @Override
    public void onConnectResult(ConnResult connResult) {
        Log.e("debug_test_onC11", "11111111");
        if (dataListener != null) {
            dataListener.onConnectResult(connResult);
        }
    }

    @Override
    public void onRecevieData(byte[] data) {
        if (dataListener != null) {
            if (data[3] == 0x04) {
                if (data[4] == 0x01) {
                    //电量的数据
                    Log.e("debug_data", String.format("%02d", (data[7] & 0xff)));
                    dataListener.getEletricity(data);
                }
            }

            //心率的数据
                dataListener.getHeartRate(data);
//            if (data[3] == 0x0a) {
//            }

        }
    }

    private DataCallback dataListener;

    //注册监听
    public void resgistData(DataCallback dataListener) {
        bleHelper.registerBleListener(this);
        this.dataListener = dataListener;
    }

    public static interface DataCallback {

        void onConnectResult(ConnResult connResult);

        //电量
        void getEletricity(byte[] data);

        //心率
        void getHeartRate(byte[] data);
    }

}
